package cn.wei.reallyenjoy.user.service.impl;

import cn.wei.reallyenjoy.common.entity.User;
import cn.wei.reallyenjoy.common.utils.MD5;
import cn.wei.reallyenjoy.user.mapper.UserMapper;
import cn.wei.reallyenjoy.user.pojo.vo.LoginVO;
import cn.wei.reallyenjoy.user.pojo.vo.RegisterVO;
import cn.wei.reallyenjoy.user.pojo.vo.UserInfoVO;
import cn.wei.reallyenjoy.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Wei
 * @since 2021-11-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public UserInfoVO login(LoginVO loginVO) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        return baseMapper.login(mobile, MD5.encrypt(password));
    }

    @Override
    public Boolean isRegister(String mobile) {
        Integer count = baseMapper.selectCount(new QueryWrapper<User>().eq("mobile", mobile));
        if (count>0){
            return true;
        }
        return false;
    }

    @Override
    public void register(RegisterVO registerVO) {
        User user = new User();
        user.setMobile(registerVO.getMobile());
        user.setNickName(registerVO.getNickName());
        user.setPassword(MD5.encrypt(registerVO.getPassword()));
        user.setHeadImg(User.headimg);
        baseMapper.insert(user);
    }
}
